﻿using System.Collections.Generic;
using System.Threading.Tasks;
using WeiboWPSdk.Json.Linq;
using WeiboWPSdk.ResponseEntity;

namespace WeiboWPSdk.Api {
    public class TagApi:ApiBase {
        public TagApi(ClientHttpHelper httpHelper)
            : base(httpHelper) {
        }

        #region 读取接口
        /// <summary>
        /// 返回指定用户的标签列表
        /// http://open.weibo.com/wiki/2/tags
        /// </summary>
        /// <param name="uid">要获取的标签列表所属的用户ID。</param>
        /// <param name="count">单页返回的记录条数，默认为20。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <returns></returns>
        public async Task<IEnumerable<Tag>> GetTagsByUIdAsync(long uid, int count = 20, int page = 1) {
            var json = await HttpHelper.GetBasicCommandAsync("tags", new WeiboParameter("uid", uid), new WeiboParameter("count", count), new WeiboParameter("page", 1));
            var list = new List<Tag>();
            foreach (var obj in JArray.Parse(json)) {
                var first = (JProperty)obj.First;
                var last = (JProperty)obj.Last;
                list.Add(new Tag {
                    Id = long.Parse(first.Name),
                    TagName = first.Value.ToString(),
                    Weight = int.Parse(last.Value.ToString())
                });
            }
            return list;
        }
        /// <summary>
        /// 批量获取用户的标签列表
        /// http://open.weibo.com/wiki/2/tags/tags_batch
        /// </summary>
        /// <param name="uids">要获取标签的用户ID。最大20，逗号分隔。</param>
        /// <returns></returns>
        public async Task<IDictionary<long, IEnumerable<Tag>>> GetTagsBatchAsync(params long[] uids) {
            var json = await HttpHelper.GetBasicCommandAsync("tags/tags_batch", new WeiboParameter("uids", string.Join(",", uids)));
            var dic = new Dictionary<long, IEnumerable<Tag>>();
            foreach (var item in JArray.Parse(json)) {
                var uid = long.Parse(item["id"].ToString());
                var list = new List<Tag>();
                foreach (var obj in item["tags"]) {
                    var first = (JProperty)obj.First;
                    var last = (JProperty)obj.Last;
                    list.Add(new Tag {
                        Id = long.Parse(first.Name),
                        TagName = first.Value.ToString(),
                        Weight = int.Parse(last.Value.ToString())
                    });
                }
                dic.Add(uid, list);
            }
            return dic;
        }
        /// <summary>
        /// 获取系统推荐的标签列表
        /// http://open.weibo.com/wiki/2/tags/suggestions
        /// </summary>
        /// <param name="count">返回记录数，默认10，最大10。</param>
        /// <returns></returns>
        public async Task<IDictionary<long, string>> GetSuggestionsAsync(int count = 10) {
            var json = await HttpHelper.GetBasicCommandAsync("tags/suggestions", new WeiboParameter("count", count));
            var dic = new Dictionary<long, string>();
            foreach (var obj in JArray.Parse(json)) {
                dic.Add(long.Parse(obj["id"].ToString()), obj["value"].ToString());
            }
            return dic;
        }
        #endregion
        #region 写入接口
        /// <summary>
        /// 为当前登录用户添加新的用户标签
        /// http://open.weibo.com/wiki/2/tags/create
        /// 无论调用该接口次数多少，每个用户最多可以创建10个标签
        /// </summary>
        /// <param name="tags">要创建的一组标签，用半角逗号隔开，每个标签的长度不可超过7个汉字，14个半角字符。</param>
        /// <returns></returns>
        public async Task<IEnumerable<long>> CreateAsync(params string[] tags) {
            var json = await HttpHelper.PostBasicCommandAsync("tags/create", new WeiboParameter("tags", string.Join(",", tags)));
            var ids = new List<long>();
            foreach (var x in JArray.Parse(json)) {
                ids.Add(long.Parse(x["tagid"].ToString()));
            }
            return ids;
        }
        /// <summary>
        /// 批量删除一组标签
        /// http://open.weibo.com/wiki/2/tags/destroy_batch
        /// 只能删除当前登录用户自己的标签
        /// </summary>
        /// <param name="tagIds">要删除的一组标签ID，以半角逗号隔开，一次最多提交10个ID。</param>
        /// <returns></returns>
        public async Task<IEnumerable<long>> DestroyBatchAsync(params long[] tagIds) {
            var json = await HttpHelper.PostBasicCommandAsync("tags/destroy_batch", new WeiboParameter("ids", string.Join(",", tagIds)));
            var list = new List<long>();
            foreach (var x in JArray.Parse(json)) {
                list.Add(long.Parse(x["tagid"].ToString()));
            }
            return list;
        }
        #endregion
    }
}
